/* @(#)GSManufactureCategoryProcessor.java    V0.1
 *
 * Project: GSdevelop
 *
 * Modify Information:
 * =============================================================================
 *   Author       Date       Description
 *   ------------ ---------- ---------------------------------------------------
 *   owen         2006-2-28
 *
 * Copyright Notice:
 * =============================================================================
 *    Copyright (c) 2001-2006 Beijing HiTRUST Technology Co., Ltd. 1808 Room, 
 *        Science & Technology Building, No. 9 South Street, Zhong Guan Cun, 
 *        Haidian District, Beijing ,100081, China All rights reserved.
 *
 *    This software is the confidential and proprietary information of
 *        Beijing HiTRUST Technology Co., Ltd. ("Confidential Information").
 *        You shall not disclose such Confidential Information and shall use it
 *        only in accordance with the terms of the license agreement you entered
 *        into with HiTRUST.
 *
 * Warning:
 * =============================================================================
 * 
 */
package com.hitrust.gs.business;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;

import system.LoginContext;

import com.hitrust.gs.bean.GSManufactureCategoryBean;
import com.hitrust.gs.dao.GSManufactureCategoryDAO;
import com.hitrust.gs.lib.GSBaseConstants;
import com.hitrust.gs.lib.GSLogWriter;
import com.hitrust.gs.service.GSBaseProcesser;
import com.hitrust.gs.util.exception.GSException;

/**
 * 产品种类
 */
public class GSManufactureCategoryProcessor implements GSBaseProcesser{
    
    /**
     * 通过产品种类编号和产品种类名称查询所有产品种类
     * String aCategoryId
     * String aCategoryName
     * @return ArrayList
     * @throws GSException
     */
    
    public static ArrayList queryManageCategory(String aCategoryId1,String aCategoryId2,String aCategoryName)throws GSException{
        GSManufactureCategoryDAO tGSManufactureCategoryDAO = new GSManufactureCategoryDAO();
        ArrayList tArrayList=new ArrayList();
        try {
            //通过产品种类编号和产品种类名称所有产品种类
            tArrayList=tGSManufactureCategoryDAO.queryManufactureCatetory(aCategoryId1,aCategoryId2,aCategoryName);
        } catch (GSException e) {
            GSLogWriter.error("GSManufactureCategoryProcessor GSException: " +e.toString());
        }
        return tArrayList;
    }
    
    /**
     * 新增产品种类
     * 
     * GSManufactureCategoryBean aGSManufactureCategoryBean
     * @throws GSException   
     */
    public static void addTask(GSManufactureCategoryBean aGSManufactureCategoryBean,LoginContext loginContext)throws GSException {
        GSManufactureCategoryDAO tGSManufactureCategoryDAO = new GSManufactureCategoryDAO();
            //判断产品种类编号是否存在
            if (tGSManufactureCategoryDAO.isExist(aGSManufactureCategoryBean.getGs_manufacture_category_id())) {
                          throw new GSException("该产品种类已经存在");
                }

            String level1=aGSManufactureCategoryBean.getGs_manufacture_level();
            if(aGSManufactureCategoryBean.getGs_upper_manufacture_category().equals("0")){
               if((aGSManufactureCategoryBean.getGs_upper_manufacture_category().equals("0")&&level1.equals("1"))==false){
                throw new GSException("该父级种类编号级别不够");
              }
            }
            else{
              GSManufactureCategoryBean tGSManufactureCategoryBean=tGSManufactureCategoryDAO.getCategory(aGSManufactureCategoryBean.getGs_upper_manufacture_category());
              String level2=String.valueOf(Integer.parseInt(tGSManufactureCategoryBean.getGs_manufacture_level())+1);
              if(!level1.equals(level2)){
                throw new GSException("该父级种类编号级别不够");
              }
            }
            //添加机构信息
            if (tGSManufactureCategoryDAO.add(aGSManufactureCategoryBean,loginContext) == false) {
                throw new GSException("");
            }
        }
    
    /**
     * 删除产品种类信息
     * 
     * @param aCategoryId 产品种类编号
     *            
     * throws CUPSException
     */
    public static void withdrawTask(String aCategoryId)throws GSException{
        GSManufactureCategoryDAO tGSManufactureCategoryDAO = new GSManufactureCategoryDAO();
        
        boolean bool = tGSManufactureCategoryDAO.isSubExist(aCategoryId);
        if(!bool){
            if(tGSManufactureCategoryDAO.delete(aCategoryId)==false){
                throw new GSException("");
            }
        }
        else throw new GSException(GSException.E20076,GSException.E20076_MSG);
    }
    
    /**
     * 通过产品种类编号得到详细信息
     * 
     * @param aCategoryId 产品种类编号
     * 
     * @throws GSException          
     */
    public static GSManufactureCategoryBean getCategory(String aCategoryId)throws GSException{
        GSManufactureCategoryDAO tGSManufactureCategoryDAO = new GSManufactureCategoryDAO();
        GSManufactureCategoryBean tGSManufactureCategoryBean=new GSManufactureCategoryBean();  
        try {
            //通过机构编号和机构名称查询所有机构信息
            tGSManufactureCategoryBean=tGSManufactureCategoryDAO.getCategory(aCategoryId);
        } catch (GSException e) {
            GSLogWriter.error("GSManufactureCategoryProcessor GSException: " +e.toString());
        }
        return tGSManufactureCategoryBean;
    }
    
    
	/**
	 * 查询最大产品编号
	 * 
	 * @return
	 */
	public static String getMaxProviderId() {
		String tRs = "10000";

		String tSql = "select * from T_PRODUCT_CATEGORY where CODE in (select max(CODE) from T_PRODUCT_CATEGORY where  code!='A0000')";
		try {
			ArrayList tList = new GSManufactureCategoryDAO().executeQuery(new GSManufactureCategoryBean(), tSql, null);
			if (tList.size()>0) {
			    tRs = ((GSManufactureCategoryBean) tList.get(0)).getGs_manufacture_category_id();
            }
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		return tRs;
	}    
    
    /**
     * 修改产品种类信息
     * 
     * @param aGSManufactureCategoryBean
     *           
     * throws GSException
     */
    public static void modifyTask(GSManufactureCategoryBean aGSManufactureCategoryBean,LoginContext loginContext) throws GSException{
        GSManufactureCategoryDAO tGSManufactureCategoryDAO = new GSManufactureCategoryDAO();
        //修改机构信息
        if(tGSManufactureCategoryDAO.modify(aGSManufactureCategoryBean,loginContext)==false){
            throw new GSException("");
        }
    }
    
    /**
     * 通过级别查询所有产品种类
     *
     * @return ArrayList
     * @throws GSException
     */
    
    public static ArrayList queryManageCategoryLevel()throws GSException{
        GSManufactureCategoryDAO tGSManufactureCategoryDAO = new GSManufactureCategoryDAO();
        ArrayList tArrayList=new ArrayList();
        try {
            //通过产品种类编号和产品种类名称所有产品种类
            tArrayList=tGSManufactureCategoryDAO.queryManufactureByLevel();
        } catch (GSException e) {
            GSLogWriter.error("GSManufactureCategoryProcessor GSException: " +e.toString());
        }
        return tArrayList;
    }
    /**
     * 取得产品种类子级
     * @param aManufCategoryId
     * @param aLevel
     * @return
     * @throws GSException
     */
    public static ArrayList querySubManufactureCategory(String aManufCategoryId,String aLevel) throws GSException{
        ArrayList tList = new ArrayList();
        Connection tConn = null;
        GSManufactureCategoryDAO tManufCategoryDAO = new GSManufactureCategoryDAO();
        try{
            tConn = tManufCategoryDAO.getConnetion();
            //查询一级产品种类下所属二,三级产品种类
            if(aLevel.equals(GSBaseConstants.LEVEL_ONE)){
                ArrayList tRs = tManufCategoryDAO.querySubManufCategory(aManufCategoryId,GSBaseConstants.LEVEL_TWO,tConn);
                if(tRs.size() > 0){
                    for(int i=0; i<tRs.size(); i++){
                        GSManufactureCategoryBean tBean = (GSManufactureCategoryBean)tRs.get(i);
                        tList.add(tBean.getGs_manufacture_category_id());
                        ArrayList tRs2 = tManufCategoryDAO.querySubManufCategory(tBean.getGs_manufacture_category_id(),GSBaseConstants.LEVEL_THREE,tConn);
                        if(tRs2.size() > 0){
                            for(int j=0; j<tRs2.size(); j++){
                                GSManufactureCategoryBean tBean2 = (GSManufactureCategoryBean)tRs2.get(j);
                                tList.add(tBean2.getGs_manufacture_category_id());
                            }
                        }
                    }
                }
            }else if(aLevel.equals(GSBaseConstants.LEVEL_TWO)){
                ArrayList tRs = tManufCategoryDAO.querySubManufCategory(aManufCategoryId,GSBaseConstants.LEVEL_THREE,tConn);
                if(tRs.size() > 0){
                    for(int i=0; i<tRs.size(); i++){
                        GSManufactureCategoryBean tBean = (GSManufactureCategoryBean)tRs.get(i);
                        tList.add(tBean.getGs_manufacture_category_id());
                    }
                }
            }
        }catch(GSException ge){
            throw ge;
        }catch(Exception e){
            GSLogWriter.error(e.toString());
            throw new GSException(GSException.E20077,GSException.E20077_MSG);
        }finally{
            try{
                tConn.close();
            }catch(SQLException se){
                se.printStackTrace();
            }
        }
        return tList;
    }
    /**
     * 通过产品种类编号或产品种类名称查询所有产品种类 String tVal
     * 
     * @return GSManufactureCategoryBean
     * @throws GSException
     */

    public GSManufactureCategoryBean queryManufactureCatetoryByIdOrName(String tVal) throws GSException {
        GSManufactureCategoryDAO tGSManufactureCategoryDAO = new GSManufactureCategoryDAO();
        GSManufactureCategoryBean tGSManufactureCategoryBean = null;
        try {
            // 通过产品种类编号和产品种类名称所有产品种类
            tGSManufactureCategoryBean = tGSManufactureCategoryDAO.queryManufactureCatetoryByIdOrName(tVal);
        } catch (GSException e) {
            GSLogWriter.error("查询产品种类错误" + e.getMessage());
        }
        return tGSManufactureCategoryBean;
    }
}






